/*
 * This class handles the configuration parameters from the system. It reads all
 * the parameters from the QRYPARAMETERS table and stores it in a hastable. This can be
 * accessed by the system. It is a wrapper around the configuration
 */

package com.querie.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Hashtable;
import com.querie.db.DBMSConnection;
/**
 *
 * @author a-skoshy
 * @author Duc On (ductuanon@hotmail.com)
 */
public class QryParameters {
    private Hashtable<String, String> htParams = null;
    //private Connection con = null;

    private static class Holder {
        static final QryParameters instance = new QryParameters();
    }

    public static QryParameters getInstance() {
        return Holder.instance;
    }

    private QryParameters() {
        Statement stmt = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            con = DBMSConnection.getConnection();
            stmt = con.createStatement();

            htParams = new Hashtable();

            String sql = "SELECT KEYPARAM, KEYVALUE FROM QRYPARAMETERS";
            //System.out.println("QryParameters sql: " + sql);

            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                htParams.put(rs.getString("KEYPARAM"), rs.getString("KEYVALUE"));
            }
            rs.close();

        }  catch (Exception ex){
            ex.printStackTrace();
        } finally {
            try {
                DBMSConnection.safeClose(rs, stmt, con);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void refresh(){
            Statement stmt = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            con = DBMSConnection.getConnection();
            stmt = con.createStatement();

            htParams = new Hashtable();

            String sql = "SELECT KEYPARAM, KEYVALUE FROM QRYPARAMETERS";
            //System.out.println("QryParameters sql: " + sql);

            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                htParams.put(rs.getString("KEYPARAM"), rs.getString("KEYVALUE"));
            }
            rs.close();

        }  catch (Exception ex){
            ex.printStackTrace();
        } finally {
            try {
                DBMSConnection.safeClose(rs, stmt, con);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public String getValue(String key) {
        return this.htParams.get(key);
    }

    public String setValue(String key, String val) {
        String sql = "update qryparameters set keyvalue=? where keyparam=?";
        PreparedStatement pstmt = null;
        Connection con = null;
        try {
            con = DBMSConnection.getConnection();
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, val);
            pstmt.setString(2, key);
            pstmt.executeUpdate();
            pstmt.executeUpdate();
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            try {
                DBMSConnection.safeClose(null, pstmt, con);
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        return this.htParams.put(key, val);
    }

    public String setSpecialValue(String key, String val) {
        String sql = "update qryparameters set keyvalue=" + val +" where keyparam=?";
        PreparedStatement pstmt = null;
        Connection con = null;

        try {
            con = DBMSConnection.getConnection();
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, key);
            pstmt.executeUpdate();
            pstmt.executeUpdate();
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            try {
                DBMSConnection.safeClose(null, pstmt, con);
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        return this.htParams.put(key, val);
    }

    public String checkPrediction(String s) {
        String p = getValue("PREDICTION");

        return ((p.compareToIgnoreCase(s) == 0) ? "CHECKED"  : "");
    }

    public String checkCollaboration(String s) {
        String p = getValue("ITEMORUSER");

        return ((p.compareToIgnoreCase(s) == 0) ? "CHECKED"  : "");
    }

    public String checkAlgo(String s) {
        String p = getValue("ALGO");
        
        if (p == null) return "";

        return ((p.compareToIgnoreCase(s) == 0) ? "CHECKED"  : "");
    }

    public String checkSeparate(String s) {
        String p = getValue("SEPARATE");

        return ((p.compareToIgnoreCase(s) == 0) ? "CHECKED"  : "");
    }

    public String checkRemove(String s) {
        String p = getValue("REMOVESAMEREC");

        if (p == null) return "";

        return ((p.compareToIgnoreCase(s) == 0) ? "CHECKED"  : "");
    }

    public void enableAllButtons() {
        setValue("BTNSAVE", "");
        setValue("BTNCLEAN", "");
        setValue("BTNTRAIN", "");
        setValue("BTNTOPK", "");
        setValue("BTNTEST", "");
        setValue("BTNVIEW", "");
    }

    public void disableAllButtons() {
        setValue("BTNSAVE", "disabled");
        setValue("BTNCLEAN", "disabled");
        setValue("BTNTRAIN", "disabled");
        setValue("BTNTOPK", "disabled");
        setValue("BTNTEST", "disabled");
        setValue("BTNVIEW", "disabled");
    }
}
